package fun.tan90.device.source.mysql.plugin;

import fun.tan90.device.source.mysql.plugin.model.BinlogClient;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
import org.noear.solon.core.event.EventBus;
import org.noear.solon.core.event.EventListener;

@Slf4j
public class SourceMysqlPlugin implements Plugin {

    AppContext context;

    @Override
    public void start(AppContext context) {
        log.info("SourceMysqlPlugin start");
        this.context = context;
        //添加自己的配置文件
        context.cfg().loadAdd("source-mysql.yml");
        //扫描自己的bean
        context.beanScan(SourceMysqlPlugin.class);
    }

    @Override
    public void stop() {
        log.info("SourceMysqlPlugin stop");
        //移除事件订阅
        context.beanForeach(bw -> {
            if (bw.raw() instanceof EventListener) {
                EventBus.unsubscribe(bw.raw());
            } else if (bw.raw() instanceof BinlogClient binlogClient) {
                binlogClient.close();
            }
        });
    }
}